package io.gitee.twoke.generator.engine.impl.persistence.entity;

import io.gitee.twoke.generator.engine.impl.cfg.GenerateEngineConfigurationImpl;
import io.gitee.twoke.generator.engine.impl.persistence.AbstractManager;
import io.gitee.twoke.generator.engine.impl.persistence.entity.data.DataManager;

public abstract class AbstractEntityManager<EntityImpl extends Entity> extends AbstractManager implements EntityManager<EntityImpl> {

  public AbstractEntityManager(GenerateEngineConfigurationImpl processEngineConfiguration) {
    super(processEngineConfiguration);
  }
  
  /*
   * CRUD operations
   */
  

  @Override
  public EntityImpl findById(String entityId) {
    return getDataManager().findById(entityId);
  }
  
  @Override
  public EntityImpl create() {
    return getDataManager().create();
  }

  @Override
  public void insert(EntityImpl entity) {
    insert(entity, true);
  }

  @Override
  public void insert(EntityImpl entity, boolean fireCreateEvent) {
    getDataManager().insert(entity);

//    ActivitiEventDispatcher eventDispatcher = getEventDispatcher();
//    if (fireCreateEvent && eventDispatcher.isEnabled()) {
//      eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_CREATED, entity));
//      eventDispatcher.dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_INITIALIZED, entity));
//    }
  }
  
  @Override
  public EntityImpl update(EntityImpl entity) {
    return update(entity, true);
  }
  
  @Override
  public EntityImpl update(EntityImpl entity, boolean fireUpdateEvent) {
    EntityImpl updatedEntity = getDataManager().update(entity);
    
//    if (fireUpdateEvent && getEventDispatcher().isEnabled()) {
//      getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_UPDATED, entity));
//    }
    
    return updatedEntity;
  }
  
  @Override
  public void delete(String id) {
    EntityImpl entity = findById(id);
    delete(entity);
  }
  
  @Override
  public void delete(EntityImpl entity) {
    delete(entity, true);
  }

  @Override
  public void delete(EntityImpl entity, boolean fireDeleteEvent) {
    getDataManager().delete(entity);
//    if (fireUpdateEvent && getEventDispatcher().isEnabled()) {
//      getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.ENTITY_UPDATED, entity));
//    }
  }
  
  protected abstract DataManager<EntityImpl> getDataManager();

}